//
//  WheelImage.m
//  轮播图练习
//
//  Created by ZC on 15/11/9.
//  Copyright © 2015年 ZC. All rights reserved.
//

#import "WheelImage.h"
#import "UIImageView+WebCache.h"

@interface WheelImage ()
@property (nonatomic, copy)NSArray *nIdArray;

@end
@implementation WheelImage

#pragma mark - 内存释放


#pragma mark - init
- (instancetype)initWithFrame:(CGRect)frame withImageArray:(NSArray *)imageNameArray withNIdArray:(NSArray *)nIdArray{
    self = [super initWithFrame:frame];
    if (self) {
        self.nIdArray = nIdArray;
        //        设定照片数量
        _imageCount = imageNameArray.count;
        //        创建scrollView
        [self creatScrollView:imageNameArray];
        //        创建pageControll
        [self creatPageControl];
        [self addSubview:self.scrollView];
        [self addSubview:self.pageControl];
//        __unused 为 未使用的对象消除警告
        __unused NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:4 target:self selector:@selector(scrollBegin) userInfo:nil repeats:YES];
    }
    return self;
}

- (void)scrollBegin {
    CGFloat x = self.scrollView.contentOffset.x;
    //    添加自定义翻页动画
    CATransition *animation = [CATransition animation];
    //    设置动画执行时间
    animation.duration = 1;
    //    设置动画类型 cube立方
    [animation setType:@"cube"];
    //    动画开始方向
    animation.subtype = kCATransitionFromRight;
    [self.scrollView.layer addAnimation:animation forKey:nil];
    [self.scrollView setContentOffset:CGPointMake(x + self.scrollView.frame.size.width, 0) animated:YES];
    if (self.scrollView.contentOffset.x == 0) {
        self.scrollView.contentOffset = CGPointMake(self.scrollView.frame.size.width * self.imageCount, 0);
    } else if (self.scrollView.contentOffset.x / self.scrollView.frame.size.width == self.imageCount + 1) {
        self.scrollView.contentOffset = CGPointMake(self.scrollView.frame.size.width, 0);
    }
    if (self.pageControl.currentPage == self.imageCount - 1) {
        self.pageControl.currentPage = 0;
    } else {
        self.pageControl.currentPage++;
    }
}

#pragma mark - creatScroll && creatPageControl
- (void)creatScrollView:(NSArray *)imageNameArray {
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
    self.scrollView.delegate = self;
    if (imageNameArray.count > 0) {
        for (int i = 0; i < imageNameArray.count + 2; i++) {
            NSMutableString *imageName = [[NSMutableString alloc] init];
            if (i == 0) {
                //          将本组照片中的最后一张照片放在最前面
                [imageName appendString:[imageNameArray lastObject]];
            } else if (i == imageNameArray.count + 1) {
                //                将本组照片中的第一张照片放在最后面
                [imageName appendString:[imageNameArray firstObject]];
            } else {
                [imageName appendString:[imageNameArray objectAtIndex:i - 1]];
            }
            UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(self.frame.size.width * i, 0, self.frame.size.width, self.frame.size.height)];
            imageView.userInteractionEnabled = YES;
            [imageView sd_setImageWithURL:[NSURL URLWithString:imageName] placeholderImage:[UIImage imageNamed:@"placeHolder"]];
            UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
            button.frame = CGRectMake(0, 0, imageView.frame.size.width, imageView.frame.size.height);
            button.backgroundColor = [UIColor clearColor];
            [button addTarget:self action:@selector(buttonDidPress:) forControlEvents:UIControlEventTouchUpInside];
            button.tag = 2000 + i;
            [imageView addSubview:button];
            [self.scrollView addSubview:imageView];
        }
        
        self.scrollView.pagingEnabled = YES;
        //        可移动宽度设为(照片数+ 2)张照片的宽度
        self.scrollView.contentSize = CGSizeMake(_scrollView.frame.size.width * (imageNameArray.count + 2), 0);
        self.scrollView.contentOffset = CGPointMake(_scrollView.frame.size.width, 0);
        self.scrollView.showsHorizontalScrollIndicator = NO;
    }
    
}

- (void)buttonDidPress:(UIButton *)sender {
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    if (sender.tag == 2001 || sender.tag == 2004) {
        [center postNotificationName:@"CaroToDetails" object:self.nIdArray[0]];
    }else if (sender.tag == 2002) {
        [center postNotificationName:@"CaroToDetails" object:self.nIdArray[1]];
    }else {
        [center postNotificationName:@"CaroToDetails" object:self.nIdArray[2]];
    }
}

- (void)creatPageControl {
    if (!self.pageControl) {
        self.pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake((self.frame.size.width - 80) / 2, self.frame.size.height - 30, 80, 30)];
        //        页数设为总照片数
        self.pageControl.numberOfPages = _imageCount;
        [self.pageControl addTarget:self action:@selector(pagecontrolAction:) forControlEvents:UIControlEventValueChanged];
    }
}

#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    if (scrollView.contentOffset.x == 0) {
        scrollView.contentOffset = CGPointMake(scrollView.frame.size.width * self.imageCount, 0);
    } else if (scrollView.contentOffset.x / scrollView.frame.size.width == self.imageCount + 1) {
        scrollView.contentOffset = CGPointMake(scrollView.frame.size.width, 0);
    }
    self.pageControl.currentPage = scrollView.contentOffset.x / scrollView.frame.size.width - 1;
}

#pragma mark - 滑动翻页时的动画效果
- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView {
    CATransition *animation = [CATransition animation];
    //    设置动画执行时间
    animation.duration = 1;
    //    设置动画类型 cube立方
    [animation setType:@"cube"];
    //    动画开始方向
    animation.subtype = kCATransitionFromRight;
    [scrollView.layer addAnimation:animation forKey:nil];
}

#pragma mark - UIPageControlAction
- (void)pagecontrolAction:(UIPageControl *)page {
    [self.scrollView setContentOffset:CGPointMake(self.scrollView.frame.size.width * (page.currentPage + 1), 0) animated:YES];
}



@end